/**
 * @Name 腾讯地图
 * @Author fanhk
 * @param {Object} exports
 */
layui.define(['jquery', 'util'], function(exports) {
	var $ = layui.$
		//字符常量
		,
		MOD_NAME = 'qqMap',
		ELEM = '.layui-qqMap',
		//外部接口
		qqMap = {
			index: layui.qqMap ? (layui.qqMap.index + 10000) : 0
				//设置全局项
				,
			set: function(options) {
					var that = this;
					that.config = $.extend({}, that.config, options);
					return that;
				}
				//事件监听
				,
			on: function(events, callback) {
				return layui.onevent.call(this, MOD_NAME, events, callback);
			}
		},
		//操作当前实例
		thisIns = function() {
			var that = this,
				options = that.config,
				id = options.id || options.index;
			return {
				reload: function(options) {
					that.reload.call(that, options);
				},
				config: options
			}
		},
		//构造器
		Class = function(options) {
			var that = this;
			that.index = ++qqMap.index;
			that.config = $.extend({}, that.config, qqMap.config, options);
			that.render();
		};

	//渲染视图
	Class.prototype.render = function() {
		var that = this,
			options = that.config;
		//options.elem = $(options.elem);
		$.ajax({ //获取插件
			url: 'https://map.qq.com/api/gljs?v=1.exp&key='+options.key,
			dataType: 'script',
			cache: true,
			async: true,
		});
		window.onload =initMap;
		function initMap() {
			console.log("初始化地图")
			var map = new TMap.Map(options.elem, {
				pitch: 0,
				zoom: 12,
				center: new TMap.LatLng(39.984104, 116.307503)
			});
			that.map = map;
			that.TMap = TMap;
			console.log(that);
			//绑定点击事件
			map.on("click",function(evt){
				var lat = evt.latLng.getLat().toFixed(6);
				var lng = evt.latLng.getLng().toFixed(6);
				//position.innerHTML = lat + "," + lng;
				console.log(evt)
			})
		}
	}

	//核心入口
	qqMap.render = function(options) {
		var ins = new Class(options);
		return thisIns.call(ins);
	};
	exports('qqMap', qqMap);
})
